package com.yy.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yy.model.VideoCommunication;
import com.yy.utils.ResponseEntity;
import com.yy.utils.data.DataUtil;
import com.yy.utils.ntes.PropertiesNtes;
import com.yy.utils.ntes.VodLive;


/**
 * 视频通讯
 * @author yy
 * @email 
 * @date 2017-04-07 13:37:57
 */
@Controller
@RequestMapping("/web/videoCommunication")
public class VideoCommunicationController {
	
	@RequestMapping("/list")
	public ModelAndView list(ModelAndView mav){
		List<VideoCommunication> videoList = new ArrayList<VideoCommunication>();
		//直播房间
		JSONObject json=DataUtil.stringJson(VodLive.channellist(PropertiesNtes.getLiveRoomMaxNumber()));
		if(DataUtil.jsonInt(json, "code")==200){
			JSONArray jsonArray = DataUtil.jsonArray(DataUtil.jsonObj(json, "ret"), "list");
			if (jsonArray.size()>0) {
				for (int i = 0; i < jsonArray.size(); i++) {
					//if((int) jsonArray.getJSONObject(i).get("status")==1){
						VideoCommunication videoCommunication = new VideoCommunication();
						videoCommunication.setCid((String) jsonArray.getJSONObject(i).get("cid"));
						videoCommunication.setName((String) jsonArray.getJSONObject(i).get("name"));
						videoCommunication.setStatus((int) jsonArray.getJSONObject(i).get("status"));
						videoCommunication.setType((int) jsonArray.getJSONObject(i).get("type"));
						videoList.add(videoCommunication);
					//}
				}
			}
		}
		mav.addObject("videoList",videoList);
		mav.setViewName("video/video");
		return mav;
	}
	
	@ResponseBody
	@RequestMapping("/videoList")
	public ResponseEntity videoList(){
		List<VideoCommunication> videoList = new ArrayList<VideoCommunication>();
		//直播房间
		JSONObject json=DataUtil.stringJson(VodLive.channellist(PropertiesNtes.getLiveRoomMaxNumber()));
		if(DataUtil.jsonInt(json, "code")==200){
			JSONArray jsonArray = DataUtil.jsonArray(DataUtil.jsonObj(json, "ret"), "list");
			if (jsonArray.size()>0) {
				for (int i = 0; i < jsonArray.size(); i++) {
					//if((int) jsonArray.getJSONObject(i).get("status")==1){
						VideoCommunication videoCommunication = new VideoCommunication();
						videoCommunication.setCid((String) jsonArray.getJSONObject(i).get("cid"));
						videoCommunication.setName((String) jsonArray.getJSONObject(i).get("name"));
						videoCommunication.setStatus((int) jsonArray.getJSONObject(i).get("status"));
						videoCommunication.setType((int) jsonArray.getJSONObject(i).get("type"));
						videoList.add(videoCommunication);
					//}
				}
			}
		}
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("videoList",videoList);
		return ResponseEntity.ok().put("map",mapData);
	}
	
	@ResponseBody
	@RequestMapping("/select")
	public ResponseEntity select(String cid){
		JSONObject json=DataUtil.stringJson(VodLive.address(cid));
		Map<String,Object> map = new HashMap<String, Object>(); 
		VideoCommunication videoCommunication = new VideoCommunication();
		if(DataUtil.jsonInt(json, "code")==200){
			JSONObject retJson =DataUtil.jsonObj(json, "ret");
			videoCommunication.setCid(cid);
			videoCommunication.setName(DataUtil.jsonString(retJson, "name"));
			videoCommunication.setRtmpPullUrl(DataUtil.jsonString(retJson, "rtmpPullUrl"));
			videoCommunication.setHlsPullUrl(DataUtil.jsonString(retJson, "hlsPullUrl"));
			videoCommunication.setHttpPullUrl(DataUtil.jsonString(retJson, "httpPullUrl"));
			videoCommunication.setPushUrl(DataUtil.jsonString(retJson, "pushUrl"));
		}
		map.put("videoCommunication",videoCommunication);
		return ResponseEntity.ok().put("map",map);
	}
	
}
